package org.zachary.agent.infrastructure.service;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class AvatarServiceImpl {
    @Value("${minio.endpoint}") private String endpoint;
    @Value("${minio.access-key}") private String accessKey;
    @Value("${minio.secret-key}") private String secretKey;

    public String uploadAvatar(MultipartFile file, String userId) throws Exception {
        MinioClient minioClient = MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();

        // 文件名格式：user_{userId}.jpg
        String objectName = "user_" + userId + ".jpg";

        minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket("avatars")
                        .object(objectName)
                        .stream(file.getInputStream(), file.getSize(), -1)
                        .contentType(file.getContentType())
                        .build()
        );

        // 返回可直接访问的URL
        return endpoint + "/avatars/" + objectName;
    }
}
